﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLL;
using Models;
using TeacherMS.Windows;

namespace TeacherMS.View
{
    public partial class ClassView : UserControl
    {
        public ClassView()
        {
            InitializeComponent();
        }

        private void ClassView_Load(object sender, EventArgs e)
        {
            var service = new ClassService();
            this.dataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
            dataGridView.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
            dataGridView.DataSource = service.Select();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var service=new ClassService();
            var @class = new Class();
            @class.Name=textBoxName.Text.Trim();
            @class.Grade = comboBoxGrade.Text.Trim();
            @class.ClassNumber = comboBoxClass.Text.Trim();
            @class.InsertDate = DateTime.Now;
            if (string.IsNullOrEmpty(@class.Name)) { 
                MessageBox.Show("名字不为空");
            }
            int count=service.Inert(@class);
            if (count > 0)
            {
                MessageBox.Show("添加成功");
                this.dataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
                dataGridView.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
                dataGridView.DataSource=service.Select();
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //for (int i = 0; i < dataGridView.ColumnCount; i++)
            //{
            //    dataGridView.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            //}
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            var result=MessageBox.Show("确定删除吗？", "提示", MessageBoxButtons.YesNo);
            if ( result != DialogResult.Yes)
            {
                return;
            }
            var rows = dataGridView.SelectedRows;
            //Console.WriteLine($"{rows.Count}****************************");
            if (rows.Count > 0)
            {
                var index = rows[0].Index;
                var list=dataGridView.DataSource as List<Class>;
                var model = list[index];
                var count=new ClassService().Delete(model);
                if (count > 0)
                {
                    var service = new ClassService();
                    MessageBox.Show("删除成功");
                    this.dataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
                    dataGridView.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
                    dataGridView.DataSource = service.Select();
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            var rows = dataGridView.SelectedRows;
            //Console.WriteLine($"{rows.Count}****************************");
            if (rows.Count > 0)
            {
                var index = rows[0].Index;
                var list = dataGridView.DataSource as List<Class>;
                var model = list[index];
                var editClassForm=new EditClassForm(model);
                var result = editClassForm.ShowDialog();
                dataGridView.DataSource = new ClassService().Select();

            }
            }
    }
}
